package ace.cmp.security.core.impl.model.principal;

import ace.cmp.security.core.impl.constant.Oauth2Constant;
import java.time.Instant;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;

/**
 * @author caspar
 * @date 2023/3/3 9:41 代表默认oauth2 token解析结果
 */
@Data
@Builder
@AllArgsConstructor
public class AceTokenIntrospectPrincipal implements OAuth2AuthenticatedPrincipal {
  private final OAuth2AuthenticatedPrincipal tokenPrincipal;

  /**
   * 是否有效
   */
  public Boolean getActive() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_ACTIVE_KEY);
  }

  /**
   * subject id
   */
  public String getSubject() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_SUBJECT_KEY);
  }

  /**
   * 令牌授予的对象
   */
  public Set<String> getAud() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_AUD_KEY);
  }

  /**
   * token 生效的时间
   */
  public Instant getNotBefore() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_NOT_BEFORE_KEY);
  }

  /**
   * scope
   */
  public Set<String> getScope() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_SCOPE_KEY);
  }

  /**
   * token的签发方。
   */
  public String getIssuer() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_ISSUER_KEY);
  }

  /**
   * 过期时间戳
   */
  public Instant getExpired() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_EXPIRED_KEY);
  }

  /**
   * 发布的时间戳
   */
  public Instant getIssuedAt() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_ISSUED_AT_KEY);
  }

  /**
   * 客户端id
   */
  public String getClientId() {
    return tokenPrincipal.getAttribute(Oauth2Constant.TOKEN_CLIENT_ID_KEY);
  }

  @Override
  public Map<String, Object> getAttributes() {
    return tokenPrincipal.getAttributes();
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return tokenPrincipal.getAuthorities();
  }

  @Override
  public String getName() {
    return tokenPrincipal.getName();
  }
}
